﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Engine;

namespace GameOfCoins
{
	public partial class Form1 : Form
	{
		CWorldCore	m_oCore;
		CPlayerCore	m_oPlayerCore;

		public Form1()
		{
			InitializeComponent();

			int iWorldWidth     = 0;
			int iWorldHeight    = 0;

			m_oCore			= new CWorldCore();
			m_oPlayerCore	= new CPlayerCore();

			m_oCore.SetLog(true);

			if(!m_oCore.LoadWorld("DebugWorld"))
				m_oCore.CreateWorld("DebugWorld");

			m_oPlayerCore.SetWorld(ref m_oCore);
			m_oPlayerCore.CreatePlayer(1, 1, 0, 0, true);
			m_oPlayerCore.CreatePlayer(2, 1, 0, 10, true);

			//Resize form
			m_oCore.GetWorldDimensions(ref iWorldWidth, ref iWorldHeight);
			Width	= iWorldWidth + 20;
			Height	= iWorldHeight + 90;

			//Resize control
			m_pbPlayboard.Width	= iWorldWidth;
			m_pbPlayboard.Height = iWorldHeight;
			CUtilities.CenterPictureBox(m_pbPlayboard);

//			m_oCore.SetUtil(Const.UTIL_COIN, 2, 0);

			//Start game
			CAnimationCore.s_pbPlayboard	= m_pbPlayboard;
//			m_oCore.SetCreationRedraw(true);

			m_oPlayerCore.StartGame();
			CAnimationCore.StartPlayboardRenderer();
		}

		//Test for changing the bricks
		private void m_pbPlayboard_MouseDown(object sender, MouseEventArgs e)
		{
			int iX = e.X / Const.GRID_DIM;
			int iY = e.Y / Const.GRID_DIM;
/*
			if(e.Button == MouseButtons.Left)
				m_oCore.SetBrick(Const.BLOCK_ROCK, iX, iY);
			else
				m_oCore.SetBrick(Const.BLOCK_ICE, iX, iY);
*/
//			Bitmap oPlayboard	= m_oCore.GetWorldImage();

			
/*			
//			GRID GENERATION FOR WORLD CREATION
			g = Graphics.FromImage(oPlayboard);
			for(int i = 0; i < Const.GRIDS_XAXIS; i++)
			{
				for(int j = 0; j < Const.GRIDS_YAXIS; j++)
					g.DrawRectangle(new Pen(Color.Black, 1), i*Const.GRID_DIM, j*Const.GRID_DIM, Const.GRID_DIM, Const.GRID_DIM);
			}
			g.Dispose();
*/

//			m_pbPlayboard.Image = oPlayboard;
		}

		//Delete world
		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			CAnimationCore.StopPlayboardRenderer();
			CAnimationCore.Clear();
			m_oPlayerCore.EndGame();

//			m_oCore.SaveWorld();
			m_oCore.DeleteWorld();
		}

		//Movement test
		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Right)
				m_oPlayerCore.MoveRight(1);
			else if(e.KeyCode == Keys.Left)
				m_oPlayerCore.MoveLeft(1);
			else if(e.KeyCode == Keys.Up)
				m_oPlayerCore.MoveUp(1);
			else if(e.KeyCode == Keys.Down)
				m_oPlayerCore.FreeFall(1, false);
		}

		private void weltLadenToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string fileName = openFileDialog1.FileName;
/*
				if (m_oCore.LoadWorldFromFile(fileName))
				{
					m_oPlayerCore.SetWorld(ref m_oCore);
				}
				else
				{
					MessageBox.Show("Welt konnte nicht geladen werden!");
				}*/
			}
		}

		private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
